package com.fang.gateway.security.service;

import com.fang.gateway.entity.bo.UserRoleBo;
import com.fang.gateway.entity.po.TbNetDisLoginRecord;
import com.fang.gateway.entity.po.TbNetDisUser;
import com.fang.gateway.security.entity.bo.WebFluxAuthoritiesBo;
import com.fang.gateway.security.mapper.AuthorityMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author Bernie_fang
 * @Date 2024/12/11 23:03
 **/
@Slf4j
@Service
public class AuthorityService implements ReactiveUserDetailsService {

    @Resource
    private AuthorityMapper authorityMapper;

    public List<UserRoleBo> queryUserRoleByUserID(String userId) {
        return authorityMapper.queryUserRoleByUserID(userId);
    }

    public TbNetDisUser findUserByUsername(String username) {
        return authorityMapper.findUserByUsername(username);
    }

    public List<String> queryUserAuthorityByUserId(String userId) {
        return authorityMapper.queryUserAuthorityByUserId(userId);
    }

    public List<WebFluxAuthoritiesBo> queryUserAuthorityByUserIdForFlux(String userId) {
        return authorityMapper.queryUserAuthorityByUserIdForFlux(userId);
    }

    public List<String> queryUserAuthorityByUsername(String username) {
        return authorityMapper.queryUserAuthorityByUsername(username);
    }

    public int addLoginRecord(String userId, String username, String result, String reason) {
        TbNetDisLoginRecord loginRecord = new TbNetDisLoginRecord();
        loginRecord.setUserId(userId);
        loginRecord.setUsername(username);
        loginRecord.setResult(result);
        loginRecord.setReason(reason);
        loginRecord.setCreatedTime(new Date());
        loginRecord.setUpdatedTime(new Date());
        return authorityMapper.insertLoginRecord(loginRecord);
    }

    @Override
    public Mono<UserDetails> findByUsername(String username) {
        // TODO:
        return null;
    }

    /**
     * Description: 根据请求路径查询所需权限
     * @Author: Bernie_fang
     * @Since: 2025/3/15 23:21
     * @param path:  请求路径
     * @return: java.util.List<java.lang.String>
     **/
    public List<String> findUrlAuthorityByUrl(String path) {
        return authorityMapper.findUrlAuthorityByUrl(path);
    }

}
